$(function () {
    var user;
    function login(data) {
        user = JSON.parse(data);
        $(".nav-user .dropdown-toggle").html("<span class=\"thumb-sm avatar pull-right m-t-n-sm m-b-n-sm m-l-sm\">" +
            "<img src=\"http://localhost:8080/" + user.head + "\" alt=\"...\"></span>" +
            user.name + "<b class=\"caret\"></b>");
        $(".btn-login").hide();
        $(".nav-user").show();
    }
    $.post("../user/login_status", function (data) {
        if ("no" != data) {
            login(data);
        } else {
            $.post("../user/auto_login", function (data) {
                if ("no" != data) {
                    login(data);
                }
            })
        }
    })

    $(".btn-login").click(function () {
        $(".popup_warn_txt").empty();
        $(".popup_txt_input").val("");
        $(".popup_password").val("");
        $(".login-mask,.login-table").show();
    });
    $(".login_close_txt").click(function () {
        $(":input").val("");
        $(".login-mask,.login-table,.reg-table").hide();
    });

    $(".js-login").click(function () {
        passLoginAction();
    });

    function passLoginAction() {
        var autologin = $(".login_popup .popup_tool_checkbox:checked").length;
        var $phone = $(".login_popup .popup_txt_input").val();
        var $password = $(".login_popup .popup_password").val();
        console.log(autologin+" "+$phone+" "+$password)
        if ($phone.length == 0) {
            $(".login_popup .popup_warn").css("visibility", "visible");
            $(".login_popup .popup_warn_txt").html("请先填手机号");
            $(".login_popup .popup_txt_input").focus();
            return;
        } else if (!$phone.match("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$")) {
            $(".login_popup .popup_warn").css("visibility", "visible");
            $(".login_popup .popup_warn_txt").html("输入的手机号格式不正确");
            $(".login_popup .popup_txt_input").focus();
            return;
        } else if ($password.length == 0) {
            $(".login_popup .popup_warn").css("visibility", "visible");
            $(".login_popup .popup_warn_txt").html("请先填密码");
            $(".login_popup .popup_password").focus();
            return;
        }
        var data = "phone="+$phone+"&password="+$password+"&autologin="+autologin;
        $.post("../user/pass_login", data, function (data) {
            if (data.length > 10) {
                login(data);
                $(".login-table,.login-mask").hide();
            } else {
                $(".popup_warn").css("visibility", "visible");
                $(".popup_warn_txt").html(data).parent().css("visibility", "visible");
                if ("请先填手机号" == data) {
                    $(".popup_txt_input").focus();
                } else if ("请先填密码" == data) {
                    $(".popup_password").focus();
                }
            }
        });
    }
    
    $(".btn-logout").click(function () {
        $.post("/user/logout", function () {
            $(".btn-login").show();
            $(".nav-user").hide();
        })
    })

    passLogin();
    function passLogin() {
        $(".btn-code-login").unbind("click").click(function () {
            $(this).attr("class", "btn-pass-login").html("切换至账号密码登录");
            $(".login_popup .popup_txt_lable").width("16%");
            $(".login_popup .popup_txt_input").width("83%").prev().html("手机号：");
            $(".login_popup .popup_password").width("57%").attr({"type":"text","placeholder":"请输入验证码"}).prev().html("验证码：");
            $(".popup_forget").html("发送验证码").width("20%");
            $(".js-login").css({"pointer-events":"none","background-color":"gray"});
            $(".popup_forget").unbind("click").click(function () {
                var autologin = $(".login_popup .popup_tool_checkbox:checked").length;
                var $phone = $(".login_popup .popup_txt_input").val();
                if ($phone.length == 0) {
                    $(".login_popup .popup_warn").css("visibility", "visible");
                    $(".login_popup .popup_warn_txt").html("请先填手机号");
                    $(".login_popup .popup_txt_input").focus();
                    return;
                } else if (!$phone.match("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$")) {
                    $(".login_popup .popup_warn").css("visibility", "visible");
                    $(".login_popup .popup_warn_txt").html("输入的手机号格式不正确");
                    $(".login_popup .popup_txt_input").focus();
                    return;
                }
                $(".login_popup .popup_warn_txt").empty();
                var data = "phone="+$phone+"&autologin="+autologin;
                $.post("../user/code_login", data, function (data) {
                    if ("send" == data) {
                        $(".popup_forget").css("pointer-events", "none");
                        countDownLogin();
                        $countDown = setInterval(countDownLogin, 1000);
                        $(".login_popup .popup_password").focus();
                        $(".js-login").css({"pointer-events":"auto","background-color":"#31c27c"});
                        $(".js-login").unbind("click").click(function () {
                            $code = $(".login_popup .popup_password").val();
                            if ($code.length == 0) {
                                $(".login_popup .popup_warn").css("visibility", "visible");
                                $(".login_popup .popup_warn_txt").html("请先填验证码");
                                $(".login_popup .popup_password").focus();
                            } else if (!$code.match("[0-9]{6}")) {
                                $(".login_popup .popup_warn").css("visibility", "visible");
                                $(".login_popup .popup_warn_txt").html("验证码错误");
                                $(".login_popup .popup_password").focus();
                            } else {
                                $(".login_popup .popup_warn_txt").empty();
                                $.post("../user/code_login_check", "code="+$code, function (data) {
                                    if (data.length > 15) {
                                        login(data);
                                        $(".login-table,.login-mask").hide();
                                        clearInterval($countDown);
                                        second = 59;
                                        $(".popup_forget").html("发送验证码").css({"pointer-events":"auto","width":"20%"});
                                    } else {
                                        $(".login_popup .popup_warn").css("visibility", "visible");
                                        $(".login_popup .popup_warn_txt").html(data);
                                        $(".login_popup .popup_password").focus();
                                    }
                                });
                            }
                        })

                        var $code = $(".login_popup .popup_password").val();
                    } else {
                        $(".login_popup .popup_warn").css("visibility", "visible");
                        $(".login_popup .popup_warn_txt").html(data);
                        $(".login_popup .popup_txt_input").focus();
                    }
                })
            })
            codeLogin();
        })
    }

    $(".popup_now_reg").click(function () {
        $(":input").val("");
        $(".login-table").hide();
        $(".reg-table").show();
    })
    $(".popup_back_login").click(function () {
        $(":input").val("");
        $(".login-table").show();
        $(".reg-table").hide();
    })
    
    $(".popup_send_code").click(function () {
        var $phone = $(".reg_popup .popup_txt_input[name=phone]").val();
        if ($phone.length == 0) {
            $(".reg_popup .popup_warn").css("visibility", "visible");
            $(".reg_popup .popup_warn_txt").html("请先填手机号");
            $(".reg_popup .popup_txt_input[name=phone]").focus();
        } else if (!$phone.match("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$")) {
            $(".reg_popup .popup_warn").css("visibility", "visible");
            $(".reg_popup .popup_warn_txt").html("输入的手机号格式不正确");
            $(".reg_popup .popup_txt_input[name=phone]").focus();
        } else {
            $(".reg_popup .popup_warn_txt").empty();
            $.post("../user/code_reg", "phone="+$phone, function (data) {
                if ("send" == data) {
                    $(".popup_send_code").css("pointer-events", "none");
                    $(".popup_code").width("53%");
                    countDownReg();
                    $countDown = setInterval(countDownReg, 1000);
                    $(".popup_code").focus();
                    $(".js-reg").css({"pointer-events":"auto","background-color":"#31c27c"});
                    $(".js-reg").unbind("click").click(function () {
                        $code = $(".popup_code").val();
                        $password = $(".reg_popup .popup_password[name=password]").val();
                        $repassword = $(".reg_popup .popup_password[name=repassword]").val();
                        $(".reg_popup .popup_warn").css("visibility", "visible");
                        if ($code.length == 0) {
                            $(".reg_popup .popup_warn_txt").html("请先填验证码");
                            $(".popup_code").focus();
                        } else if (!$code.match("[0-9]{6}")) {
                            $(".reg_popup .popup_warn_txt").html("验证码错误");
                            $(".popup_code").focus();
                        } else if (!$password.match("(?![0-9]+$)(?![a-zA-Z]+$)(?!([^(0-9a-zA-Z)]|[\(\)])+$)([^(0-9a-zA-Z)]|[\(\)]|[a-zA-Z]|[0-9]){6,16}")) {
                            $(".reg_popup .popup_warn_txt").html("密码需由6-16个字母、数字或符号中至少两种组成");
                            $(".reg_popup .popup_password[name=password]").focus();
                        } else if ($password != $repassword) {
                            $(".reg_popup .popup_warn_txt").html("两次输入的密码不一致");
                            $(".reg_popup .popup_password[name=repassword]").focus();
                        } else {
                            $(".reg_popup .popup_warn_txt").empty();
                            var data = "code="+$code+"&password="+$password;
                            $.post("../user/code_reg_check", data, function (data) {
                                if ("register success" == data) {
                                    clearInterval($countDown);
                                    second = 59;
                                    $(".popup_send_code").html("发送验证码").css({"pointer-events":"auto","width":"20%"});
                                    $(".popup_back_login").click();
                                    $(".login_popup .popup_txt_input").val($phone);
                                    alert("注册成功！进入登录页面");
                                } else {
                                    $(".reg_popup .popup_warn").css("visibility", "visible");
                                    $(".reg_popup .popup_warn_txt").html(data);
                                    $(".popup_code").focus();
                                }
                            });
                        }
                    })

                    var $code = $(".login_popup .popup_password").val();
                } else {
                    $(".reg_popup .popup_warn").css("visibility", "visible");
                    $(".reg_popup .popup_warn_txt").html(data);
                    $(".reg_popup .popup_txt_input").focus();
                }
            })
        }
    })
    
    var second = 59;
    var $countDown;
    function countDownLogin() {
        if (second < 0) {
            clearInterval($countDown);
            second = 59;
            $(".popup_forget").html("发送验证码").css({"pointer-events":"auto","width":"20%"});
        } else {
            $(".popup_forget").width("25%").html(second+"s后重新发送");
            second -= 1;
        }
    }

    function countDownReg() {
        if (second < 0) {
            clearInterval($countDown);
            second = 59;
            $(".popup_send_code").html("发送验证码").css({"pointer-events":"auto","width":"20%"});
        } else {
            $(".popup_send_code").width("25%").html(second+"s后重新发送");
            second -= 1;
        }
    }

    function codeLogin() {
        $(".btn-pass-login").unbind("click").click(function () {
            $(this).attr("class", "btn-code-login").html("切换至手机验证码登录");
            $(".popup_txt_lable").width("12%");
            $(".popup_txt_input").width("87%").prev().html("帐号：");
            $(".popup_password").width("70%").attr({"type":"password","placeholder":"请输入密码"}).prev().html("密码：");
            $(".popup_forget").html("忘记密码").width("52px");
            $(".popup_warn_txt").empty();
            $(".js-login").css({"pointer-events":"auto","background-color":"#31c27c"});
            $(".js-login").unbind("click").click(function () {
                passLoginAction();
            });

            passLogin();
        })
    }


})